//
//  AddressVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/23.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "AddressVC.h"
#import "AddressView.h"
#import "NumView.h"
//收货地址
#define ADDRESS_URL @"/api/ApiUser/getReceivingAddr"
@interface AddressVC ()
{
    // 收货人
    NSString *add_shouhuoren;
    // 联系手机
    NSString *add_mobile;
    // 联系座机号
    NSString *add_tell;
    // 联系QQ
    NSString *add_qq;
    // 省
    NSString *add_sheng;
    // 市
    NSString *add_shi;
    // 县
    NSString *add_xian;
    //详细地址
    NSString *add_xiangxi;
    // 街道地址
    NSString *add_jiedao;
    // 收货地址
    NSString *add_addr;
    // 邮编
    NSString *add_youbian;
    // 充值手机号码
    NSString *add_recharge_mobile;
    // 充值QQ
    NSString *add_recharge_qq;
    // 充值支付宝帐号
    NSString *add_recharge_alipay;
    
    AddressView *addressView;
    NumView *numView;
}
@end

@implementation AddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = QBB_GRAY_COLOR;
    // Do any additional setup after loading the view.
    [self loadCustomNaviBar];
}
- (void)viewWillAppear:(BOOL)animated
{
    //请求收货地址
    [self requestForAddress];
}
//加载导航条
- (void)loadCustomNaviBar
{
    UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 20+44)];
    naviBar.backgroundColor = QBB_RED_COLOR;
    //返回按钮
    PublicBackBtn *backBtn = [[PublicBackBtn alloc]initWithVC:self andBackType:pop Img:[UIImage imageNamed:@"icon_back_white_press"]];
    [naviBar addSubview:backBtn];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 25)];
    titleLab.center = CGPointMake(WYMWITH/2, 42);
    titleLab.text = @"收货地址";
    titleLab.font = [UIFont systemFontOfSize:TITLE_SIZE];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor whiteColor];
    //分割线
    UILabel *fenge = [[UILabel alloc]initWithFrame:CGRectMake(0, 63.5, WYMWITH, 0.5)];
    fenge.backgroundColor = [UIColor lightGrayColor];
    [naviBar addSubview:fenge];
    [naviBar addSubview:titleLab];
    [self.view addSubview:naviBar];
}
//请求收货地址
- (void)requestForAddress
{
    [WYPublic addHudToWindow];
    NSDictionary *parmars = @{@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,ADDRESS_URL] Parameters:parmars successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        NSDictionary *info = dic[@"data"];
        if (ret.intValue == 0) {
            add_addr = info[@"addr"];
            add_jiedao = info[@"jiedao"];
            add_mobile = info[@"mobile"];
            add_qq = info[@"qq"];
            add_recharge_alipay = info[@"recharge_alipay"];
            add_recharge_mobile = info[@"recharge_mobile"];
            add_recharge_qq = info[@"recharge_qq"];
            add_sheng = info[@"sheng"];
            add_shi = info[@"shi"];
            add_xiangxi = info[@"xiangxi"];
            add_shouhuoren = info[@"shouhuoren"];
            add_tell = info[@"tell"];
            add_xian = info[@"xian"];
            add_youbian = info[@"youbian"];
            //更新界面
            [self updateView];
        }else{
            if(ret.intValue>2000 && ret.intValue < 3000){
                [[WYMainTabBarVC shareMainTaBarVC].navigationController popViewControllerAnimated:NO];
                //用户过期，强制重新登录 先删除用户信息
                [UserInfo deleteUserInfo];
                //登录
                [WYPublic ifUserHaveLogin];
                [WYPublic showMessageWithTitle:@"提示：" Msg:@"登录信息已过期"];
            }
            
        }

        [WYPublic removeHudToWindow];
    } failedBlock:^{
        
        [WYPublic removeHudToWindow];
    } needLog:NO];
}
//更新界面
- (void)updateView
{
    [addressView removeFromSuperview];
    [numView removeFromSuperview];
    addressView = [[[NSBundle mainBundle]loadNibNamed:@"AddressView" owner:self options:nil]lastObject];
    if ([add_shouhuoren class] == [NSNull class] || add_shouhuoren.length == 0) {
        addressView.frame = CGRectMake(0, 74, WYMWITH, 40);
        [addressView hiddenSubViews:YES];
    }else{
        addressView.frame = CGRectMake(0, 74, WYMWITH, addressView.frame.size.height);
        [addressView fillViewWithName:add_shouhuoren Phone:add_mobile Address:[NSString stringWithFormat:@"%@%@%@%@",add_sheng,add_shi,add_xian,add_xiangxi] Sheng:add_sheng Shi:add_shi Xian:add_xian XiangXi:add_xiangxi];
        [addressView hiddenSubViews:NO];
    }
    [self.view addSubview:addressView];
    
    numView = [[[NSBundle mainBundle]loadNibNamed:@"NumView" owner:self options:nil]lastObject];
    
    if ([WYPublic getUsefullStr:add_recharge_mobile].length!=0 || [WYPublic getUsefullStr:add_recharge_qq].length!=0 || [WYPublic getUsefullStr:add_recharge_alipay].length!=0) {
        numView.frame = CGRectMake(0, CGRectGetMaxY(addressView.frame)+20, WYMWITH, numView.frame.size.height);
        [numView fillViewWithPhone:add_recharge_mobile QQ:add_recharge_qq ZFB:add_recharge_alipay];
        [numView hiddenSubViews:NO];
    }else{
        numView.frame = CGRectMake(0, CGRectGetMaxY(addressView.frame)+20, WYMWITH, 40);
        [numView hiddenSubViews:YES];
    }

    
    [self.view addSubview:numView];
    
    //上架前处理
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSNumber *flag = [user objectForKey:@"user_show_pay"];
    if ([flag intValue] == 0) {
        numView.hidden = YES;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
